package com.longcai.controller.scm;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.scm.ClyjCoupon;
import com.longcai.entity.scm.ClyjCouponDetail;
import com.longcai.entity.sys.SysRoleUser;
import com.longcai.entity.sys.SysUser;
import com.longcai.service.clyjuser.ClyjUserService;
import com.longcai.service.scm.ClyjCouponDetailService;
import com.longcai.service.scm.ClyjCouponService;
import com.longcai.service.scm.ClyjScmGoodsService;
import com.longcai.util.UUIDUtil;
import com.longcai.util.UserUtil;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 礼品券表 前端控制器
 * </p>
 *
 * @author lwk
 * @since 2019-04-29
 */
@Controller
@RequestMapping("/clyjCoupon")
public class ClyjCouponController {
	private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjScmGoodsService clyjGoodsService;
    @Autowired
    private ClyjUserService userService;
    @Autowired
    private ClyjCouponDetailService couponDetailService;
    @Autowired
    private ClyjCouponService couponService;
    
    /**
     * 验证手机是否存在
     * @param phone	手机号
     * @return
     */
    @RequestMapping(value="validatePhone")
    @ResponseBody
    public ApiResult validatePhone(String phone) {
    	QueryWrapper<ClyjUser> w = new QueryWrapper<ClyjUser>();
    	w.eq("phone", phone);
    	List<ClyjUser> selectList = userService.selectList(w);
    	if(selectList==null || selectList.size()==0) 
            return ApiResult.fail("该手机号尚未注册");
         return ApiResult.success();
    }
    
    /**
     * 发放礼品卷
     * @param userPhone	手机号
     * @param sendPrice	发放金额
     * @return
     */
    @RequestMapping(value="sendCoupon")
    @ResponseBody
    public ApiResult sendCoupon(ClyjCouponDetail couponDetail) {
    	String userPhones = couponDetail.getUserPhone();
    	BigDecimal sendPrice = couponDetail.getSendPrice();
    	if(userPhones==null || userPhones.equals(""))
    		return ApiResult.fail("手机号不能为空");
    	//获取商户id
    	String userId = UserUtil.getLoginUser().getUserId();
//	    String userId="123";
    	String[] phones = userPhones.split(",");
    	try{
	    	for (String phone : phones) {
	    		QueryWrapper<ClyjCoupon> w = new QueryWrapper<ClyjCoupon>();
	    		w.eq("sh_id", userId);
	    		w.eq("user_phone", phone);
	    		List<ClyjCoupon> selectOne = couponService.selectList(w);
	    		if(selectOne==null || selectOne.size()==0){
	    			//不存在，新增
	    			ClyjCoupon c = new ClyjCoupon();
	    			c.setUserPhone(phone);
	    			c.setShId(userId);
	    			c.setCreateTime(LocalDateTime.now());
	    			c.setUpdateTime(LocalDateTime.now());
	    			c.setPriceSum(sendPrice);
	    			c.setCouponStatus("1");
	    			String uuid = UUIDUtil.uuid();
	    			c.setCouponId(uuid);
	    			couponService.insert(c);
	    			couponDetail.setCouponId(uuid);
	    			
	    		}else{
	    			//存在，修改金额
	    			ClyjCoupon clyjCoupon = selectOne.get(0);
	    			clyjCoupon.setUpdateTime(LocalDateTime.now());
	    			clyjCoupon.setPriceSum(clyjCoupon.getPriceSum().add(sendPrice));
	    			couponService.updateById(clyjCoupon);
	    			couponDetail.setCouponId(clyjCoupon.getCouponId());
	    		}
	    		//新增明细表
	    		couponDetail.setCreateTime(LocalDateTime.now());
	    		couponDetail.setDetailId(UUIDUtil.uuid());
	    		couponDetail.setDetailStatus("1");
	    		couponDetail.setShId(userId);
	    		couponDetail.setSendPrice(sendPrice);
	    		couponDetail.setUserPhone(phone);
	    		couponDetailService.insert(couponDetail);
			}
	         return ApiResult.success();
    	}catch(Exception e){
    		e.printStackTrace();
    		return ApiResult.fail("发放失败"+e.getMessage());
    	}
    }
    
    /**
     * 礼品券明细列表
     * @param 
     * @return
     */
    @RequestMapping(value="getComponList")
    @ResponseBody
    public ApiResult getComponList() {
    	//获取商户id
//    	String userId = UserUtil.getLoginUser().getUserId();
	    String userId="123";
    	try{
    		QueryWrapper<ClyjCouponDetail> w = new QueryWrapper<ClyjCouponDetail>();
    		w.eq("sh_id", userId);
    		w.orderByDesc("create_time");
    		List<ClyjCouponDetail> selectOne = couponDetailService.selectList(w);
    		if(selectOne==null || selectOne.size()==0)
    			return ApiResult.fail("暂无数据");
    		for (ClyjCouponDetail clyjCouponDetail : selectOne) {
    			ClyjCoupon selectById = couponService.selectById(clyjCouponDetail.getCouponId());
    			clyjCouponDetail.setAllPrice(selectById.getPriceSum());
			}
	        return ApiResult.success(selectOne);
    	}catch(Exception e){
    		e.printStackTrace();
    		return ApiResult.fail("发放失败"+e.getMessage());
    	}
    }
    
}

